package ntu.edu.cn.action;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Morven Qiu on 8/15/2016.
 */
public class ClassAction extends BaseAction{

    private static final Logger logger = Logger.getLogger(ClassAction.class);
    private String depId;
    //the semester
    private String xq;
    //the class number
    private String bjid;

    /**
     * query all academies
     */
    public void queryAcademy(){
        HttpGet httpGet = new HttpGet("http://jwgl.ntu.edu.cn/cjcx/Data/Basis/dep.aspx?_dc=1471227774212");
        try(CloseableHttpClient httpClient = getClosableHttpClient()) {
            HttpResponse re = httpClient.execute(httpGet);
            getWriter().print(EntityUtils.toString(re.getEntity()));
        }catch (Exception e){
            logger.error("something is error to get data ");
            getWriter().print("error");
        }
    }

    /**
     * query all class
     */
    public void queryClass(){
        HttpPost httpPost = new HttpPost("http://jwgl.ntu.edu.cn/cjcx/Data/Basis/class.aspx");
        List<NameValuePair> params = new ArrayList();
        params.add(new BasicNameValuePair("depId", depId));
        try(CloseableHttpClient httpClient = getClosableHttpClient()){
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
            httpPost.setEntity(entity);
            HttpResponse re = httpClient.execute(httpPost);
            getWriter().print(EntityUtils.toString(re.getEntity()));
        }catch (Exception e){
            logger.error("something is error to get data QueryClass");
            getWriter().print("error");
        }
    }

    /**
     * query timetable according the semester and class
     */
    public void queryTimetable(){
        HttpPost httpPost = new HttpPost("http://jwgl.ntu.edu.cn/cjcx/Data/Table/ClassTableData.aspx");
        List<NameValuePair> params = new ArrayList();
        params.add(new BasicNameValuePair("xq", xq));
        params.add(new BasicNameValuePair("bjid",bjid));
        try(CloseableHttpClient httpClient = getClosableHttpClient()){
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
            httpPost.setEntity(entity);
            HttpResponse re = httpClient.execute(httpPost);
            getWriter().print(EntityUtils.toString(re.getEntity()));
        }catch (Exception e){
            logger.equals("something is error to get data timetable");
            getWriter().print("error");
        }
    }

    public String getDepId() {
        return depId;
    }

    public void setDepId(String depId) {
        this.depId = depId;
    }

    public String getXq() {
        return xq;
    }

    public void setXq(String xq) {
        this.xq = xq;
    }

    public String getBjid() {
        return bjid;
    }

    public void setBjid(String bjid) {
        this.bjid = bjid;
    }
}
